package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.dto.UserExtraCostDTO;
import com.lin.entity.CarUserBill;
import com.lin.entity.CompInformation;
import com.lin.entity.User;
import com.lin.entity.UserExtraCost;
import com.lin.mapper.CarUserBillMapper;
import com.lin.mapper.CompInformationMapper;
import com.lin.mapper.UserExtraCostMapper;
import com.lin.service.UserExtraCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.vo.DataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2021-11-24
 */
@Service
public class UserExtraCostServiceImpl extends ServiceImpl<UserExtraCostMapper, UserExtraCost> implements UserExtraCostService {

    @Autowired
    UserExtraCostMapper userExtraCostMapper;

    @Autowired
    CompInformationMapper compInformationMapper;

    @Autowired
    CarUserBillMapper carUserBillMapper;

    QueryWrapper<UserExtraCost> queryWrapper;

    DataVO<UserExtraCost> dataVO;

    IPage<UserExtraCost> iPage;

    @Override
    public DataVO<UserExtraCost> getAllUserExtraCost(Integer page, Integer limit) {
        dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        iPage = new Page<>(page, limit);
        IPage<UserExtraCost> userExtraCostIPage = userExtraCostMapper.selectPage(iPage, null);
        dataVO.setCount(userExtraCostIPage.getTotal());
        dataVO.setData(userExtraCostIPage.getRecords());
        return dataVO;
    }

    @Override
    public DataVO<UserExtraCost> getOwnerExtraCost(Integer page, Integer limit, String orderSerial) {
        dataVO = new DataVO<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_serial", orderSerial);
        IPage<UserExtraCost> userExtraCostIPage = userExtraCostMapper.selectPage(iPage, queryWrapper);
        dataVO.setCount(userExtraCostIPage.getTotal());
        dataVO.setData(userExtraCostIPage.getRecords());
        return dataVO;
    }

    /**
     * 用户还车
     * @param orderSerial 订单编号
     * @param lastDay 剩余时间(用于后期业务扩展逾期罚款)
     * @return
     */
    @Override
    public int returnCar(String orderSerial, String lastDay) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_serial", orderSerial);
        if (userExtraCostMapper.selectOne(queryWrapper) == null) {
            UserExtraCost userExtraCost = new UserExtraCost();
            userExtraCost.setOrderSerial(orderSerial);
            userExtraCost.setLastDay(lastDay);
            return userExtraCostMapper.insert(userExtraCost);
        } else {
            return 3;
        }
    }

    /**
     * 开具详细罚款信息
     * @param userExtraCostDTO 接收到前端的罚款DTO
     * @return
     */
    @Override
    public int modifyReturnPrice(UserExtraCostDTO userExtraCostDTO) {
        //获取DTO的订单编号
        String orderSerial = userExtraCostDTO.getOrderSerial();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_serial", orderSerial);
        //通过queryWrapper获得UserExtraCost对象
        UserExtraCost userExtraCost = userExtraCostMapper.selectOne(queryWrapper);
        //BeanUtils工具，进行相同变量名的赋值
        BeanUtils.copyProperties(userExtraCostDTO, userExtraCost);
        //设置通知状态
        userExtraCost.setState("已通知");
        int update = userExtraCostMapper.updateById(userExtraCost);
//        carUserBillMapper.deleteById(userExtraCostDTO.getOrderSerial());
        //实例化CompInformation对象
        CompInformation compInformation = new CompInformation();
        //设置属性
        compInformation.setOrderSerial(userExtraCostDTO.getOrderSerial());
        compInformation.setCompDesc(userExtraCostDTO.getCompDesc());
        compInformation.setCompPrice(userExtraCostDTO.getTotalPrice());

        QueryWrapper<CarUserBill> wrapper = new QueryWrapper<>();
        wrapper.eq("order_serial", orderSerial);
        CarUserBill carUserBill = carUserBillMapper.selectOne(wrapper);
        //获取订单编号
        compInformation.setUserId(carUserBill.getUserId());
        int insert = compInformationMapper.insert(compInformation);
        return insert;
    }

}
